package com.rental.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rental.common.login.GetIdByToken;
import com.rental.common.result.AjaxResult;
import com.rental.common.util.AuthContextHolder;
import com.rental.model.entity.UserAccount;
import com.rental.model.entity.house.RequiredHouse;
import com.rental.model.form.house.RequiredHouseForm;
import com.rental.model.form.house.RequiredHousePage;
import com.rental.model.form.house.RequiredReportForm;
import com.rental.model.vo.house.RequiredHouseVo;
import com.rental.web.service.RequiredHouseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Log
@RestController
@RequestMapping("/required")
@Tag(name = "求租房屋信息", description = "个人期望租房信息，可接受价格区间，具体地理位置")
public class RequiredHouseController {

    @Autowired
    private RequiredHouseService requiredHourseService;



    /**
     * 添加个人期望租房信息，可接受价格区间，具体地理位置
     *
     * @param requiredHouseForm
     * @return
     */
    @Operation(summary = "个人期望租房信息，可接受价格区间，具体地理位置")
    @GetIdByToken
    @PostMapping("/addRequiredHouse")
    public AjaxResult<Boolean> addRequiredHouse(@RequestBody RequiredHouseForm requiredHouseForm) {
        Long userId = AuthContextHolder.getUserId();
        requiredHouseForm.setUid(userId);
        return AjaxResult.success(requiredHourseService.addRequiredHouse(requiredHouseForm));
    }

    /**
     * 分页查询个人期望房租列表
     */
    @Operation(summary = "分页查询个人期望房租列表")
    @PostMapping("/getRequiredHouseList")
    @GetIdByToken
    public AjaxResult<Page<RequiredHousePage>> getRequiredHouseList(
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestBody(required = false) RequiredHouseForm requiredHouseForm
    ){
        return AjaxResult.success(requiredHourseService.getRequiredHouseList(pageNum, pageSize, requiredHouseForm));
    }


    /**
     * 查询个人求租详情信息
     *
     * @param id
     * @return
     */

    @Operation(summary = "查询个人求租详情信息")
    @GetMapping("/getRequiredHouseDetail/{id}")
    @GetIdByToken

    public AjaxResult<RequiredHouseVo> getRequiredHouseDetail(@PathVariable("id") Long id) {
        return AjaxResult.success(requiredHourseService.getRequiredHouseDetail(id));
    }


    /**
     * 下架个人求租信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "下架个人求租信息")
    @GetMapping("/deleteRequiredHouse/{id}")

    @GetIdByToken

    public AjaxResult<Boolean> deleteRequiredHouse(@PathVariable("id") Long id) {
        return AjaxResult.success(requiredHourseService.deleteRequiredHouse(id));
    }

    /**
     * 根据Id查询个人求组信息
     *
     * @param id
     * @return
     */
    @Operation(summary = "根据Id查询个人求组信息")
    @GetIdByToken
    @GetMapping("/getRequiredHouseById/{id}")
    public AjaxResult<RequiredHouseVo> getRequiredHouseById(@PathVariable("id") Long id) {
        Long userId = AuthContextHolder.getUserId();
        return AjaxResult.success(requiredHourseService.getRequiredHouseById(id, userId));
    }


    /**
     * 修改个人求租信息
     *
     * @param requiredHouseForm
     * @return
     */
    @Operation(summary = "修改个人求租信息")
    @PostMapping("/updateRequiredHouse")
    @GetIdByToken

    public AjaxResult<Boolean> updateRequiredHouse(@RequestBody RequiredHouseForm requiredHouseForm) {
        return AjaxResult.success(requiredHourseService.updateRequiredHouse(requiredHouseForm));
    }


    /**
     * 点击举报时候将信息保存
     */
    @Operation(summary = "点击举报时候将信息保存")
    @PostMapping("/reportRequiredHouse")
    @GetIdByToken
    public AjaxResult<Boolean> reportRequiredHouse(@RequestBody RequiredReportForm requiredReportForm) {
        return AjaxResult.success(requiredHourseService.reportRequiredHouse(requiredReportForm));
    }

    @Operation(summary = "是否收藏成功")
    @GetMapping("/collect/{rid}/{uid}")
    @GetIdByToken
    public AjaxResult<Boolean> collect(@PathVariable("rid") Long rid, @PathVariable("uid") Long uid) {
        return AjaxResult.success(requiredHourseService.collect(rid, uid));
    }

    /**
     *  取消收藏
     * @param rid
     * @param uid
     * @return
     */

    @Operation(summary = "取消收藏")
    @GetMapping("/cancelCollect/{rid}/{uid}")
    @GetIdByToken
    public AjaxResult<Boolean> cancelCollect(@PathVariable("rid") Long rid, @PathVariable("uid") Long uid) {
        return AjaxResult.success(requiredHourseService.cancelCollect(rid, uid));
    }


}
